package com.qs.aop;

import sun.misc.ProxyGenerator;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 使用jdk提供的ProxyGenerator类来生成jdk动态代理时生成的class字节码文件
 *
 * @author qinyupeng
 * @since 2021-07-10 15:13:39
 */
public class ProxyGeneratorUtils {

	public static void writeProxyClassToHardDisk(String path) {
		// 获取代理类的字节码
		byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy12", IndexServiceImpl.class.getInterfaces());

		FileOutputStream out = null;

		try {
			out = new FileOutputStream(path);
			out.write(classFile);
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args) {
		ProxyGeneratorUtils.writeProxyClassToHardDisk("/Users/qinyupeng/ideaProject/sourceCode/spring-framework-5.1.x/qs-aop/out/$Proxy12.class");
	}
}
